<script type="text/javascript">
	function toggle(source) {
	  checkboxes = document.getElementsByName('checked[]');
	  for(var i=0, n=checkboxes.length;i<n;i++) {
		checkboxes[i].checked = source.checked;
	  }
	}
	
	function checkEmail() {
		var email = document.getElementsByClassName('email');
		var len = email.length;
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		for (i = 0; i < len; i++) {
			if (!filter.test(email[i].value)) {
				var myerror = '<span style="color:red; font-size:10pt;">Sai định dạng MAIL</span>';
				var error = document.getElementsByClassName('z-email');
				error[i].innerHTML = myerror;
				return false;
			} 
			else {
				var error = document.getElementsByClassName('z-email');
				error[i].innerHTML = '';
			}
		}
		return true;
 	}

	function checkSDT() {
		var sdt = document.getElementsByClassName('sdt_gv_get');
		var len = sdt.length;
		var phoneno = /^(01\d{9})|(0[0-9]\d{8})$/; 
		for (i = 0; i < len; i++) { 
			if(!(sdt[i].value.match(phoneno))) {
				var myerror = '<span style="color:red">Sai định dạng SĐT</span>';
				var error = document.getElementsByClassName('z-sdt');
				error[i].innerHTML = myerror;
				return false;
			}
			else {
				var error = document.getElementsByClassName('z-sdt');
				error[i].innerHTML = '';
			}
		}
		return true;
 	}
		
	function Validation() {
		if(checkEmail() && checkSDT()) {
			return true;
		}
		else return false;
	}
	
	$(function () {
			
		$('#choice').html('');
		$('#btn_save_modfify').hide();
		
		$('#btn_modify').click(function() {
			//var value = $('.checkall').parent('tr').find(':checkbox').attr('checked', this.checked);
			$('#edit').show();
			$(this).hide();
		});
		
		$('#btn_delete').click( function() {
			var num_check = $('input[type=checkbox]:checked').size();
			if(num_check > 0) return confirm("Bạn có chắc chắn muốn xóa không?");
		});
		
		///////////////////////////////////////////////////////////////////////////////
		///Chọn chỉnh sua thong tin sinh vien
		$('#edit').click( function() {
			var sl = 0;
			if ($('input[type=checkbox]:checked').size() > 0)
			{
				$('#btn_add').attr('disabled', 'disabled');
				$('#btn_delete').attr('disabled', 'disabled');
			}
			
			$('input[type=checkbox]').each( function(e) {
				//$(this).parent('td').parent('tr').find($('span.txtModify')).removeClass('edit');
				//$(this).parent('td').parent('tr').find($('span.pModify')).removeClass('edit');	
				$(this).parent('td').parent('tr').find($('span.txtModify')).hide();
				$(this).parent('td').parent('tr').find($('span.pModify')).show();
   			 });
			
			$('input[type=checkbox]:checked').each( function(e) {
				
				//$(this).parent('td').parent('tr').find($('span.txtModify')).addClass('edit');
				//$(this).parent('td').parent('tr').find($('span.pModify')).addClass('edit');

				$(this).parent('td').parent('tr').find($('span.txtModify')).show();
				$(this).parent('td').parent('tr').find($('span.pModify')).hide();
				sl = $(this).size();
   			 });
			 
			if (sl > 0) {
				$('#choice').html('');	
				$('#btn_save_modfify').show();
				$('#edit').hide();
				
			}
			else {
				$('#choice').html('Vui lòng chọn sinh viên');
			}
		});
		
		
		$('#btn_add').click(function () {
			//$('.container').hide();
			$('.khung').css('opacity','0.5');
			$('#show_add').show(500);
			$('.show_add').css('opacity','1');
		});
		
		$('.huybo').click(function () {
			$('.khung').css('opacity','1');
			$('#show_add').hide();
		});
		
		//Hien thi lai form add neu du lieu khong lop le (FORM THEM SINH VIEN)
		var pre_add = $('#hidden_add').attr('value');
		if (pre_add == 1) {
			
			$('#show_add').show();
			$('.khung').css('opacity','0.5');
		}
			
		//Hien thi lai form cap nhat neu du lieu khong lop le (FORM CAP NHAT SINH VIEN)
		var pre_modify = $('#hidden_modify').attr('value');
		if (pre_modify == 1) {
			$('#show_add').hide();
			$('.container').show();
			$('#choice').html('');	
			$('#btn_modify').show();
			$('#edit').hide();
				
		}
		
		
	});
</script>


<!-- ----------------------------------------------------------------------------------
                    COMPOSE ADD TEACHER MODAL 
-------------------------------------------------------------------------------------->

<div id="show_add" tabindex="-1" aria-hidden="true" style="position:absolute; display:none">
  <div class="modal-dialog">
      <div class="modal-content">
          <div class="modal-header">
              <button id="btn-close" type="button" class="close huybo" data-dismiss="modal" aria-hidden="true">&times;</button>
              <h4 class="modal-title"><i class="fa fa-envelope-o"></i> Thêm giảng viên</h4>
          </div>
           
         <form action="" method="post">
          <table id="table_add" border="0">
                    <tr>
                    	<td><label>MSCB (*)</label></td>
                        <td><input type="text" class="form-control" name="add[mscb]" placeholder="Nhập mã số giảng viên" value="<?php echo set_value('mscb'); ?>" /> 
						<?php echo form_error('mscb'); ?></td>
                    </tr>
                  
                  <tr>
                      <td><label>Họ tên (*)</label></td>
                      <td><input type="text" class="form-control" name="add[hoten]" placeholder="Nhập họ tên" value="<?php echo set_value('hoten'); ?>"/>
                      <?php  echo form_error('hoten'); ?>
                      </td>   
                  </tr>
                  
                  <tr><td><label>Giới tính (*)</label></td><td><select class="form-control" name="add[gioitinh]">
                          <option>Nam</option>
                          <option>Nữ</option>
                      </select></td> 
                  </tr>
                  
                  <tr><td><label style="display:inline">Ngày sinh (*)</label></td><td><div class="input-group" style="">
                          <input name="add[ngaysinh]" type="date" class="form-control" placeholder="yyyy/mm/dd" value="<?php echo set_value('ngaysinh'); ?>"/>
                      </div><!-- /.input group -->
                      <div><?php echo form_error('ngaysinh'); ?></div></td>            
  				
                  </tr>
               

                  <tr><td><label>Địa chỉ (*)</label></td><td><textarea name="add[diachi]" class="form-control" rows="3" placeholder="Nhập địa chỉ"><?php echo set_value('diachi'); ?></textarea>
                      <?php echo form_error('diachi');?></td>
                   </tr>


                  <tr><td> <label>Số điện thoại (*)</label></td><td><input name="add[sodt]" type="text" class="form-control" placeholder="Nhập số điện thoại" value="<?php echo set_value('sodt'); ?>"/>
                     <?php echo form_error('sodt'); ?>
</td>
				</tr>                      
                  

                  <tr><td><label>Mail (*)</label></td><td><input name="add[mail]" type="text" class="form-control" placeholder="ai_do@examble.com"/>
                      <?php echo form_error('mail');?></td></tr>                      
                      
                  <tr><td><label>Bộ môn (*)</label></td><td><?php 
					  		if (isset($LayBoMon) && !empty($LayBoMon)) {
								echo "<select class='form-control' name='add[ma_bm]'>";
								foreach ($LayBoMon as $temp) {
									echo "<option value='".$temp['ma_bm']."'>".$temp['ten_bm']."</option>";
								}
								echo "</select>";
							}
					  ?></td>
                    </tr>
                      

                  <tr><td><label>Trình độ (*)</label></td><td><?php 
					  		if (isset($LayTrinhDo) && !empty($LayTrinhDo)) {
								echo "<select class='form-control' name='add[ma_td]'>";
								foreach ($LayTrinhDo as $temp) {
									echo "<option value='".$temp['ma_td']."'>".$temp['ten_td']."</option>";
								}
								echo "</select>";
							}
					  ?></td>
                      </tr>
                      

                  <tr><td><label>Quyền (*)</label></td><td><select class="form-control" name="add[maquyen]">
                          <option value="2">Mức 2 (mức giảng viên)</option>
                          <option value="1">Mức 1 (mức admin)</option>
                          <option value="3">Mức 3 (mức sinh viên)</option>
                      </select></td>
                   </tr>

                  <tr>
                  	<td colspan="2"><span style="color: red">(*) Là các trường bắt buộc nhập</span><input type="hidden" name="data[press]" id="hidden_add" value="<?php echo $press_add ?>" /></td>
                  </tr>

                  <tr>
                      <td colspan="2" class="btn-save-cancel">
                      <input id="btn_save" name="add_teacher" type="submit" class="btn btn-primary pull-left" value="Lưu lại"/>
                      <input id="btn_huybo" name="huybo" type="close" class="btn btn-primary huybo" value="Hủy bỏ" />
                      </td>
                  </tr>
           </table>
        </form> 
      </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->








<form action="" method="post">
<!-- NOI DUNG -->
<div class="khung" style="font-family:'Times New Roman'; ">
    <div class="row">
    <div class="nguoidung">
 <div class="box">
    <div class="box-body table-responsive"> 
        <input type="hidden" value="<?php echo $press_modify; ?>" id="hidden_modify" />
        <table id="example1" class="table-bordered table-hover table_nguoidung">

               <thead>
                   <tr>
                    <th colspan="11" class="student_header">Danh sách giảng viên</th>
                   </tr>
                   <tr>
                   	   <th><input type="checkbox" name="checked[]" onclick="toggle(this)" value="" class="checkall"/></th>
                       <th>STT</th>
                       <th>MSCB</th>
                       <th>Họ tên</th>
                       <th>Giới tính</th>
                       <th>Ngày sinh</th>
                       <th>Địa chỉ</th>
                       <th>Số điện thoại</th>
                       <th>Mail</th>
                       <th>Bộ môn</th>
                       <th>Trình độ</th>
                   </tr>
               </thead>
			<tbody>
				<?php
                    session_start();
                    $user = new mgiangvien();
                    $i = 1;
                    foreach ($ds as $user)
                    {
                          echo "<tr>";  
                          echo "<td><input type='checkbox' name='checked[]' value='$user->mscb'/></td>";  
                          echo "<td>$i</td>";
						           

                          echo "<td>$user->mscb</td>";                
                          echo "<td>
						  			<span class='pModify'>$user->hoten</span>
						  			<span class='txtModify' hidden>
						  			<input class='hoten_gv' type='text' name='$user->mscb[hoten]' value='$user->hoten'/></span>
								</td>";	
						  
                          echo "<td>
									<span class='pModify'>$user->gioitinh</span>
									<span class='txtModify' hidden>
									<select name='$user->mscb[gioitinh]'>
										<option value='Nam'";
										if ($user->gioitinh == 'Nam') echo 'selected';
						  echo                        ">Nam</option>
										<option value='Nữ'";
										if ($user->gioitinh == 'Nữ') echo 'selected';
						  echo                       ">Nữ</option>
									</select></span></td>"; 
						          
                          echo "<td>
								  <span class='pModify'>$user->ngaysinh</span>
								  <span class='txtModify' hidden >
								  <input class ='ngaysinh_gv' required type='date' name='$user->mscb[ngaysinh]' value='$user->ngaysinh'/></span>
						  		</td>";
						  
						     
                          echo "<td class='diachi_gv'>
						  		  <span class='pModify' >$user->diachi</span>
								  <span class='txtModify' hidden >
								  <input class='diachi_gv' type='text' name='$user->mscb[diachi]' value='$user->diachi'/></span>".form_error('$user->mscb[diachi]').
						  		"</td>";  
						  
                        echo "<td class='sdt_gv'>
						  		  <span class='pModify'>$user->sdt</span>
								  <span class='txtModify' hidden>
								  <input class='sdt_gv_get sdt_gv' type='text' name='$user->mscb[sdt]' value='$user->sdt' onblur='checkSDT()' onkeyup='checkSDT()'/></span><div class='z-sdt'></div>
						  		</td>";  

					    echo "<td class='mail_gv'>
						  		  <span class='pModify' >$user->mail</span>
								  <span class='txtModify' hidden '>
								  <input class='email' type='text' name='$user->mscb[mail]' value='$user->mail' onblur='checkEmail()' onkeyup='checkEmail()'/></span><div class='z-email'></div>
						  		</td>";  
						
						//Hien thi nganh hoc  
					    echo "<td class='nganhhoc'>
						  		  <span class='pModify'>$user->ten_bm</span>
								  <span class='txtModify' hidden>";
								  if (isset($LayBoMon) && !empty($LayBoMon)) {
						echo       "<select class='bomon_gv' name='$user->mscb[ma_bm]' style='width:130px;'>";
								    foreach ($LayBoMon as $temp) {
										
						echo            "<option value='".$temp['ma_bm']."'";
						 				  if ($user->ten_bm === $temp['ten_bm']) echo 'selected';
						echo				                      ">".$temp['ten_bm']."</option>";
								    }
						echo       "</select>";
							      }
						echo       "</span></td>";		  
							
						//Hien thi trinh do	
						echo "<td class='trinhdo'>
						  		  <span class='pModify'>$user->ten_td</span>
								  <span class='txtModify' hidden>";
								  if (isset($LayTrinhDo) && !empty($LayTrinhDo)) {
						echo       "<select class='trinhdo_gv'  name='$user->mscb[ma_td]'>";
								    foreach ($LayTrinhDo as $temp) {
										
						echo            "<option value='".$temp['ma_td']."'";
						 				  if ($user->ten_td === $temp['ten_td']) echo 'selected';
						echo				                      ">".$temp['ten_td']."</option>";
								    }
						echo       "</select>";
							      }
						echo       "</span></td>";
						
							   
														       
                          echo "</tr>";     
                        $i++;
                    }
                ?>
           
            </tbody>
            <tfoot>
                   <tr>
                   	   <th></th>
                       <th>STT</th>
                       <th>MSCB</th>
                       <th>Họ tên</th>
                       <th>Giới tính</th>
                       <th>Ngày sinh</th>
                       <th>Địa chỉ</th>
                       <th>Số điện thoại</th>
                       <th>Mail</th>
                       <th>Bộ môn</th>
                       <th>Trình độ</th>
                   </tr>
            </tfoot>
        </table>
       
    </div><!-- /.box-body -->
  </div><!-- /.box -->
         
         <div id="choice" class="active"></div>
         <div class="row btn_border">
			<div class="col-md-2"></div>
			
         	<div class="col-md-8 btn_nguoidung">
				<input class="btn btn-default" type="submit" value="Hủy bỏ" />
				<input id="btn_add" class="btn btn-default" value="Thêm" />
                <input id ="edit" name="edit" type="button" class="btn btn-default" value="Cập nhật"/>
                <input id="btn_save_modfify" name="btn_save" type="submit" class="btn btn-default" onclick="return Validation()" value="Lưu lại" />
				<input id="btn_delete" name="btn_delete" type="submit" class="btn btn-default" value="Xóa" />
                <div style="background-color:#CCC"></div>
            
			</div>
			
			<div class="col-md-2"></div>
         </div><!--end div row -->
         
        </div>
        <!--end div col-md-12-->

        
    	</div><!--end div row-->
  
	</div><!--end div contener-->
 </form>





       